package com.feng.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/**
 * html工具类
 * @author 
 *
 */
public class HtmlUtil {
	
	/**
	 *  获取html字符串
	 * @param urlStr url地址
	 * @param charSet 编码格式
	 * @return
	 */
	public static String getHtmlStrByUrl(String urlStr,String charSet){
		StringBuffer stringBuffer = new StringBuffer();
		URL url = null;
		URLConnection urlConnection = null;
		InputStreamReader ins = null;
		BufferedReader bufferedReader =null;
		try {
			url = new URL(urlStr);
			urlConnection = url.openConnection();
			urlConnection.setConnectTimeout(3000);
			ins = new InputStreamReader(urlConnection.getInputStream(), charSet);
			bufferedReader =  new BufferedReader(ins);
			String tmp = null;
			while((tmp=bufferedReader.readLine())!=null){
				stringBuffer.append(tmp+"\n");
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return stringBuffer.toString();
	}
	
	
	public static String parseHtml(String html){
		StringBuffer stringBuffer2 = new StringBuffer();
		Document document = Jsoup.parse(html);
		Elements elements = document.getElementsByClass("list-short-article");
		if(!elements.isEmpty() && elements.size()>0){
			Element articleDiv = elements.get(0);
			Elements lis = articleDiv.getElementsByTag("li");
			for (Element li : lis) {
				Elements tmp = li.getElementsByTag("a");
				stringBuffer2.append(tmp.get(1).text()+"\n");
			}
		}
		return stringBuffer2.toString();
	}
	public static List<String> parseHtml2(String html){
		List<String> list = new ArrayList<String>();
		Document document = Jsoup.parse(html);
		Elements elements = document.getElementsByClass("list-short-article");
		if(!elements.isEmpty() && elements.size()>0){
			Element articleDiv = elements.get(0);
			Elements lis = articleDiv.getElementsByTag("li");
			for (Element li : lis) {
				Elements tmp = li.getElementsByTag("a");
				list.add(tmp.get(1).text());
			}
		}
		return list;
	}
	
	

}
